package com.ynsj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynsj.mapper.WorkIssueMapper;
import com.ynsj.domain.WorkIssue;
import com.ynsj.service.WorkIssueService;
import org.springframework.util.StringUtils;

@Service
public class WorkIssueServiceImpl extends ServiceImpl<WorkIssueMapper, WorkIssue> implements WorkIssueService{

    /**
     * 回复工单
     *
     * @param id
     * @param answer
     * @return
     */
    @Override
    public boolean updateAnswer(Long id, String answer) {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        return update(new LambdaUpdateWrapper<WorkIssue>()
                .eq(id!=null,WorkIssue::getId,id)
                .set(!StringUtils.isEmpty(answer),WorkIssue::getAnswer,answer)
                .set(WorkIssue::getAnswerUserId,Long.valueOf(userId)));
    }

    /**
     * 分页查询客服工单
     *
     * @param page
     * @param status
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Page<WorkIssue> findByPage(Page<WorkIssue> page, Integer status, String startTime, String endTime) {
        page.addOrder(OrderItem.desc("last_update_time"));
        return page(page,new LambdaQueryWrapper<WorkIssue>()
                .eq(status!=null,WorkIssue::getStatus,status)
                .between(!StringUtils.isEmpty(startTime)&&!StringUtils.isEmpty(endTime),WorkIssue::getCreated,startTime,endTime+" 23:59:59"));
    }
}
